﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using System.Collections;
using DevExpress.XtraBars.Helpers;
using Hdt.TransOcean.Dal;


namespace Hdt.TransOcean.Presentation
{
    public partial class FormMain : XtraForm
    {
        public bool IsLoggedOut = false;

        public FormMain()
        {
            InitializeComponent();
            barManager.ForceInitialize();
            SkinHelper.InitSkinPopupMenu(mPaintStyle);
            InitBars();
            InitTabbedMDI();
            if (GlobalVariable.loginUser != null)
            {
                sLoginInfo.Caption = GlobalVariable.loginUser.code.ToString();
            }
        }

        const string formName = "fromName";
        //Cursor currentCursor;
        int oldActive = -2;
        private void InitBars()
        {
            /////bsiDocName.Caption = (ActiveMDIForm != null) ? ActiveMDIForm.Tag.ToString() : " ";

            //iFont.Enabled = IsActiveTextRTFForm;
            //InitEdit();
            //InitPicture();
            if (oldActive == NumActive) return;
            //iClose.Enabled = mEdit.Enabled = ActiveMDIForm != null;
            //iRotateBy90.Enabled = iHorizontalFlip.Enabled = iVerticalFlip.Enabled =
            //    iPictureAlignment.Enabled = sizeMode.Enabled = IsActiveImageForm;
            //iSelectAll.Enabled = colorEdit.Enabled = IsActiveTextForm;

            //for (int i = mEdit.ItemLinks.Count - 1; i >= 0; i--)
            //    mEdit.RemoveLink(mEdit.ItemLinks[i]);

            //if (IsActiveImageForm)
            //{
            //    iPicture.ImageIndex = 13;
            //    mEdit.AddItem(iHorizontalFlip);
            //    mEdit.AddItem(iVerticalFlip);
            //    mEdit.AddItem(iRotateBy90);
            //    mEdit.Caption = "Image &Edit";
            //}
            //if (IsActiveTextForm)
            //{
            //    iPicture.ImageIndex = 14;
            //    mEdit.AddItem(iCut);
            //    mEdit.AddItem(iCopy);
            //    mEdit.AddItem(iPaste);
            //    mEdit.AddItem(iSelectAll).BeginGroup = true;
            //    mEdit.Caption = "Text &Edit";
            //}

            //if (ActiveMDIForm == null)
            //{
            //    iPicture.ImageIndex = -1;
            //    mEdit.Caption = "&Edit";
            //}

            oldActive = NumActive;
        }

        bool IsTabbedMdi {
            get 
            {
            /////    return biTabbedMDI.Down; 
                return true;
            } 
        }
        void InitTabbedMDI()
        {
            xtraTabbedMdiManager1.MdiParent = IsTabbedMdi ? this : null;
            //biCascade.Visibility = iTileHorizontal.Visibility = iTileVertical.Visibility = IsTabbedMdi ? BarItemVisibility.Never : BarItemVisibility.Always;
        }

        private int NumActive
        {
            get
            {
                if (IsActiveForm) return 1;

                return -1;
            }
        }

        private Form ActiveMDIForm
        {
            get { return this.ActiveMdiChild; }
        }
        private bool IsActiveForm
        {
            get { return (ActiveMDIForm != null && ActiveMDIForm.AccessibleName == formName); }
        }

        public void OpenMDIForm(Form formChild)
        {

            int count = xtraTabbedMdiManager1.Pages.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    if (xtraTabbedMdiManager1.Pages[i].Text == formChild.Text)
                    {
                        xtraTabbedMdiManager1.SelectedPage = xtraTabbedMdiManager1.Pages[i];
                        return;
                    }
                }
            }
            xtraTabbedMdiManager1.MdiParent = this;
            formChild.MdiParent = this;
            formChild.Show();
        }

        
        private void invoiceItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormInvoice(EOperation.New));
        }

        private void itemItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormItem(EOperation.New));
        }

        private void journeyItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormJourney(EOperation.New));
        }

        private void airlineItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormAirline(EOperation.New));
        }

        private void iCustomerItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormCustomerList());
        }

        private void iItemItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormItem(EOperation.New));
        }

        private void iJourneyItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormJourney(EOperation.New));
        }

        private void iAirlineItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormAirline(EOperation.New));
        }

        private void groupItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {

        }

        private void userItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {

        }

        private void roleItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {

        }

        private void sysConfigItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {

        }

        private void customerDebtItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormReportCongNo());
        }

        private void salesItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {

        }

        private void iCustomerList_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormCustomerList());
        }

        private void iItemList_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormItemList());
        }

        private void iJourneyList_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormJourneyList());
        }

        private void iAirlineList_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormAirlineList());
        }

        private void customerItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormCustomer(EOperation.New));
        }

        private void ReturnInvoice_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new ReturnInvoice(EOperation.New));
        }

        private void invoiceItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormInvoice(EOperation.New));
        }

        private void itemItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormItem(EOperation.New));
        }

        private void airlineItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormAirline(EOperation.New));
        }

        private void employeeItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormEmployee(EOperation.New));
        }

        private void iEmployeeList_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormEmployeeList());
        }

        private void iEmployeeItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormEmployee(EOperation.New));
        }

        private void logoutItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            
            if (MessageBox.Show("Bạn muốn đóng ứng dụng phải không? ", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.IsLoggedOut = true;
                GlobalVariable.loginUser = null;
                this.Close();
            }
        }

        private void beginningDebtTermItem_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new OpeningBalance(EOperation.New));
        }

        private void iInvoiceList_LinkPressed(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenMDIForm(new FormInvoiceList());
        }
    }
}